package org.jdarwinbots.io.dsl;

import static org.junit.Assert.*;

import java.io.*;
import java.util.List;

import org.jdarwinbots.logic.DNA;
import org.jdarwinbots.logic.DefaultOperators;
import org.jdarwinbots.logic.Operator;
import org.junit.Test;


public class DarwinBotsGrammar2ReaderTests {
	private DNA readDNA(String fileName) throws IOException {
		File file = new File(fileName);
		InputStream in = new FileInputStream(file);
		return new DarwinBotsGrammar2Reader().read(in);
		
	}
	
	private void print(DNA dna) {
		boolean f = true;
		System.out.print("DNA: ");
		for (Operator op:dna.getOperators()) {
			if (f) f = false;
			else System.out.print(',');
			System.out.print(op);
		}
		System.out.println();
	}
	
	@Test
	public void test1() throws Exception {
		DNA dna = readDNA("parsetests/sampleA.txt");
		List<Operator> ops = dna.getOperators();
		assertEquals(DefaultOperators.getIntOperator(42),ops.get(0));
		assertEquals(DefaultOperators.getIntOperator(10),ops.get(1));
		assertEquals(DefaultOperators.getIntOperator(20),ops.get(2));
		assertEquals(DefaultOperators.getOperator("add"),ops.get(3));
		assertEquals(DefaultOperators.getIntOperator(30),ops.get(4));
		assertEquals(DefaultOperators.getOperator("!="),ops.get(5));
		
		assertEquals(6,ops.size());
	}
	
	@Test
	public void test2() throws Exception {
		DNA dna = readDNA("parsetests/sampleB.txt");
		print(dna);
		List<Operator> ops = dna.getOperators();
		assertEquals(DefaultOperators.getIntOperator(10),ops.get(0));
		assertEquals(DefaultOperators.getIntOperator(1),ops.get(1)); // defined by alias .value
		assertEquals(DefaultOperators.getOperator("store"),ops.get(2));
		assertEquals(DefaultOperators.getOperator("false"),ops.get(3));
		assertEquals(DefaultOperators.getRefOperator(1),ops.get(4)); // defined by alias .value
		assertEquals(DefaultOperators.getIntOperator(10),ops.get(5));
		assertEquals(DefaultOperators.getOperator("="),ops.get(6));
		
		assertEquals(7,ops.size());
	}

	
}
